import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import login from '@/views/login.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/login', component: login },
  { path: '/reg', component: () => import('@/views/Reg.vue') },
  {
    path: '/',
    // redirect: 'home',
    component: () => import('@/views/Main.vue'),
    children: [
      { path: 'art-cate', component: () => import('@/views/ArtCate.vue') },
      { path: 'home', component: () => import('@/views/Home.vue') }
    ]
  }

]

const router = new VueRouter({
  routes
})
// 全局前置导航守卫 （判断token是否为空）
router.beforeEach((to, from, next) => {
  if (to.path === '/login' || to.path === '/reg') {
    next()
  } else {
    if (store.state.user.token) {
      next() // 表示允许访问
    } else {
      next('/login')
    }
  }
})
export default router
